/**
 * Labox有限公司项目InnovationServer
 * Labox版权所有 @copyright 2010
 * @javaclass CultivationId.java @author 丁宁 @time 2010-2-3
 */
package labox.innovation.gameserver.model.base;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * 这个类定义玩家所能升级到的仙魔位
 *
 */
public final class PracticeId {

	private static final Map<Integer, PracticeId> practiceIds = new HashMap<Integer, PracticeId>();

	/**
	 * 仙魔位所属的种族
	 */
	private final Race _race;

	/**
	 * 当前仙魔位下一级
	 */
	private PracticeId _child;

	/**
	 * 仙魔位的ID
	 */
	private final int _id;

	/**
	 * 仙魔位的名称
	 */
	private final String _name;

	/**
	 * 仙魔位的境界名称
	 */
	private final String _thought;

	/**
	 * 提升当前仙魔位需要的等级
	 */
	private int _condLevel;

	/**
	 * 提升当前仙魔位需要的经验
	 */
	private int _condExp;

	/**
	 * 提升当前仙魔位需要的银文
	 */
	private int _condMoneySliver;

	/**
	 * 提升当前仙魔位需要的道具
	 */
	private Item[] _condItem;

	private PracticeId(int id, Race r, String name, String thought) {
		_id = id;
		_race = r;
		_name = name;
		_thought = thought;
	}

	/**
	 * 返回种族<BR><BR>
	 */
	public final Race getRace() {
		return _race;
	}

	/**
	 * 返回仙魔位ID<BR><BR>
	 */
	public final int getId() {
		return _id;
	}

	/**
	 * 返回仙魔位名称<BR><BR>
	 */
	public final String getName() {
		return _name;
	}

	/**
	 * 返回仙魔位境界<BR><BR>
	 */
	public final String getThought() {
		return _thought;
	}

	/**
	 * 返回下一级称号<BR><BR>
	 *
	 */
	public final PracticeId getChild() {
		return _child;
	}

	public final boolean isParent(PracticeId p) {
		if (_race != p.getRace())
			return false;
		return _id >= p.getId();
	}

	public final boolean isParent(int id) {
		return isParent(getPractice(id));
	}

	public final boolean canPracticeLevelUp(FuzePcInstance player) {
		if (_child == null)
			return false;
		if (_child._condLevel > player.getLevel())
			return false;
		if (_child._condExp > player.getExp())
			return false;
		if (_child._condMoneySliver > player.getMoneySilver())
			return false;

		for (int i = -1; ++i < _child._condItem.length;) {
			L2ItemInstance item = player.getInventory().getItemByItemId(_child._condItem[i].itemId);
			if (item == null)
				return false;
			if (_child._condItem[i].count > item.getCount())
				return false;
		}
		
		player.addExp(-_child._condExp);
		player.reduceMoneySilver(null, _child._condMoneySliver, null, true);
		for (int i = -1; ++i < _child._condItem.length;) {
			L2ItemInstance item = player.getInventory().getItemByItemId(_child._condItem[i].itemId);
			player.destroyItem("", item, null, true);
		}

		return true;
	}

	public static final PracticeId getPractice(int id) {
		return practiceIds.get(id);
	}

	public static final void loadPractice() {
		try {
			File f = new File(Config.DATAPACK_ROOT, "/data/practice.xml");
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(new FileInputStream(f));
			Element root;
			root = document.getDocumentElement();
			if (root.getTagName().compareTo("Practices") != 0) {
				throw new Exception("Not a Practice file!");
			}
			NodeList nl = root.getElementsByTagName("Practice");
			PracticeId p;
			int id;
			String name;
			String thought;
			int race;
			for (int i = -1; ++i < nl.getLength();) {
				Element e = (Element) nl.item(i);
				id = Integer.parseInt(e.getAttribute("id"));
				name = e.getAttribute("name");
				thought = e.getAttribute("thought");
				race = Integer.parseInt(e.getAttribute("race"));
				p = new PracticeId(id, Race.values()[race], name, thought);
				p._condLevel = Integer.parseInt(e.getAttribute("level"));
				p._condExp = Integer.parseInt(e.getAttribute("exp"));
				p._condMoneySliver = Integer.parseInt(e.getAttribute("sliver"));
				name = e.getAttribute("item");
				if (name.length() > 0) {
					String[] items = name.split(";");
					p._condItem = new Item[items.length];
					for (int j = -1; ++j < items.length;) {
						String[] item = items[j].split(",");
						p._condItem[j] = p.new Item();
						p._condItem[j].itemId = Integer.parseInt(item[0]);
						p._condItem[j].count = Integer.parseInt(item[1]);
					}
				}

				practiceIds.put(id, p);
			}
			for (int i = -1; ++i < nl.getLength();) {
				Element e = (Element) nl.item(i);
				id = Integer.parseInt(e.getAttribute("id"));
				practiceIds.get(id)._child = practiceIds.get(Integer.parseInt(e.getAttribute("prac")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private class Item {
		int itemId;

		int count;
	}
}
